package com.rolerealm.controller;

import com.rolerealm.common.result.Result;
import com.rolerealm.common.util.JwtUtil;
import com.rolerealm.common.util.ResultUtil;
import com.rolerealm.domain.vo.UserPreferenceVO;
import com.rolerealm.service.UserPreferenceService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users/preferences")
@RequiredArgsConstructor
public class UserPreferenceController {

    private final JwtUtil jwtUtil;
    private final UserPreferenceService userPreferenceService;

    @GetMapping
    public Result<UserPreferenceVO> getPreferences(@RequestHeader(name = "Authorization", required = false) String authorization) {
        Long userId = jwtUtil.extractUserIdFromAuthorization(authorization);
        return ResultUtil.success(userPreferenceService.getPreferences(userId));
    }

    @PutMapping
    public Result<Void> updatePreferences(@RequestHeader(name = "Authorization", required = false) String authorization,
                                          @RequestBody @Valid UserPreferenceVO request) {
        Long userId = jwtUtil.extractUserIdFromAuthorization(authorization);
        userPreferenceService.updatePreferences(userId, request);
        return ResultUtil.success();
    }
}


